<div class="weadmin-body">

    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" lay-event="batch_delete"><i class="layui-icon"></i>批量删除</button>
            <button class="layui-btn" lay-event="create">
                <i class="layui-icon"></i>添加用户
            </button>
        </div>
    </script>

    <script type="text/html" id="opt_bar">
        {{# if(d.username != 'admin') {  }}
        <a href="javascript:;" title="编辑" lay-event="modify">
            <i class="layui-icon">&#xe642;</i>
        </a>
        <a href="javascript:;" title="删除" lay-event="delete">
            <i class="layui-icon">&#xe640;</i>
        </a>
        {{#  } }}
    </script>

    <table id="admin_table" lay-filter="admin_table"></table>

</div>
<script>
    layui.extend({
        admin: '<?php echo Yii::app()->baseUrl?>/static/admin/js/admin'
    });
    layui.use(['table','jquery','admin'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var load_index = layer.load();
        table.render({
            elem: '#admin_table',
            method: 'post',
            url: '<?php echo $this->createUrl('admin/list')?>', //数据接口
            page: true, //开启分页
            toolbar: '#toolbar',
            id: 'admin_table',
            defaultToolbar:[],
            loading:false,
            cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'username', title: '用户名',align:'center'},
                {field: 'name', title: '所属角色',align:'center'},
                {fixed: 'right', title:'操作', toolbar: '#opt_bar',align:'center'}
            ]],
            done:function (data) {
                layer.close(load_index);
            }
        });

        table.on('toolbar(admin_table)', function (obj) {
            var check_status = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'create' :
                    WeAdminShow('添加','<?php echo $this->createUrl('admin/create')?>');
                    break;
                case 'batch_delete':
                    batch_delete(check_status.data);
                    break;
                default:
                    return;
            }
        });
        table.on('tool(admin_table)', function (row) {
            var id = row.data.id;
            if (!id) {
                layer.msg('参数非法', {time:1500, icon:2});
                return ;
            }
            switch (row.event) {
                case 'delete':
                    layer.confirm('确认删除：' + row.data.username,function (index) {
                        layer.close(index);
                        delete_one(id);
                    });
                    break;
                case 'modify' :
                    WeAdminShow('编辑','<?php echo $this->createUrl('admin/modify')?>?id='+id);
                    break;
                case 'view' :
                    location.href = '<?php echo $this->createUrl('flash/list') ?>?pid='+id;
                    break;
                default :
                    return;
            }
        });

        function batch_delete(data) {
            if (data.length == 0) {
                layer.msg('未选中任何数据',{time:1500,icon:2});
                return;
            }
            layer.confirm('确认批量删除？', function (index) {
                layer.close(index);
                var delete_index = layer.load();
                var id_list = [];
                data.forEach(function (value) {
                    id_list.push(value.id);
                });
                var url = '<?php echo $this->createUrl('admin/batchDelete')?>';
                $.post(url,{id_list:id_list}, function (data) {
                    layer.close(delete_index);
                    if (data.code == 0) {
                        layer.msg(data.message,{time:800, icon:1});
                        table.reload('admin_table',{
                            page:{curr:1},
                        });
                    } else {
                        layer.msg(data.message, {time:2000,icon:2});
                    }
                });
            });
        }

        function delete_one(id) {
            $.get('<?php echo $this->createUrl('admin/remove')?>', {id: id}, function (ret) {
                if (ret.code == 0) {
                    layer.msg(ret.message, {time: 1500, icon: 1});
                    table.reload('admin_table');
                } else {
                    layer.msg(ret.message, {time: 2000, icon: 2});
                }
            });
        }
    });
</script>